﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Web.SessionState;
using System.Net;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Security.Permissions;
using System.Web.Services.Description;
using System.Xml;

namespace wsMoiGioiNamA
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {

        //khoi tao ket noi csdl
        static string strconn = WebConfigurationManager.ConnectionStrings["Connectionstring"].ConnectionString;
        SqlConnection newConn = new SqlConnection(strconn);

        [WebMethod(EnableSession = true)]
        public string Login(string strUsername, string strPassword)
        {
            string kq = "0";
            newConn.Open();

            SqlCommand comm = new SqlCommand("SELECT TenDangNhap,MatKhau,LoaiTK,MaTheThanhToan FROM ThanhVien WHERE TenDangNhap=@username and MatKhau=@password", newConn);

            SqlParameter user = new SqlParameter("@username", strUsername.Trim());
            SqlParameter pass = new SqlParameter("@password", strPassword.Trim());

            comm.Parameters.Add(user);
            comm.Parameters.Add(pass);

            SqlDataReader reader = comm.ExecuteReader();
            //return ket qua

            if (reader.HasRows)
            {
                reader.Read();
                Session["LoaiTK"] = reader.GetString(2).Trim();
                Session.Timeout = 200;
                string mathe = reader.GetString(3).Trim();
                reader.Close();
                newConn.Close();
                kq = generateRandomString(20);
                Session[kq] = mathe;
                Session.Timeout = 200;
                Session["username"] = strUsername.Trim();
                Session.Timeout = 200;
               
            }

            return kq;

        }

        [WebMethod(EnableSession = true, Description = "rut tien cho kinh doanh")]
        public int TranferMoneyForCustomer(string sid, string CustomerCreditCard, int imount)
        {
            string maTheKhachHang = CustomerCreditCard.Trim();
            if (Session[sid.Trim()] == null)
                return -1; //chua dang nhap hoac qua thoi gian dang nhap
            
            if (Session["LoaiTK"].ToString() != "2" && Session["LoaiTK"].ToString()!="4")// neu la tk ca nhan
                    return -2; // khong co quyen goi service nay
            //end kiem tra login
            // add webservice cua thay

            string username = Session["username"].ToString();
            cardsystem.EMVServices card = new cardsystem.EMVServices();
            card.CookieContainer = new CookieContainer();

            //kiem tra ma the gui thuoc ngan hang nao
            string MaNGHangGui = card.GetBankCommercialNameOfCreditCard(maTheKhachHang);
            if (MaNGHangGui == "CardNotExist")
                return -3; //ma the gui ko hop le
            //end kt ma the gui

            //kiem tra ma the nhan thuoc ngan hang nao
            string maTheKinhdoanh = Session[sid].ToString().Trim();
            string MaNGHangNhan = card.GetBankCommercialNameOfCreditCard(maTheKinhdoanh);// thay bang the cua kinh doanh
            if (MaNGHangNhan == "CardNotExist")
                return -4; //ma the nhan ko hop le
            //end kiem tra ma the nhan

            //lay ma the thanh toan quoc te cua ngan hang cua nguoi gui
            newConn.Open();
            SqlCommand comm = new SqlCommand("SELECT TenDangNhap,MatKhau,MaTheThanhToan FROM TaiKhoan WHERE MaNganHang=@manganhang", newConn);

            SqlParameter para = new SqlParameter("@manganhang", MaNGHangGui);

            comm.Parameters.Add(para);
            SqlDataReader reader = comm.ExecuteReader(); // lay thong tin tai khoan theo Ma ngan hang gui

            if (!reader.HasRows) // neu k co ngan hang
                return -5; // ngan hang gui k co trong danh sach lien ket

            reader.Read();
            string userMoiGioi_Gui = reader.GetString(0).Trim(); //login vao ngan hang cua nguoi gui-->thuc hien rut tien
            string passMoiGioi_Gui = reader.GetString(1).Trim();
            string maTheMoiGioi_Gui = reader.GetString(2).Trim();
            reader.Close();
            // end lay ma the TTQT gui

            // lay ma the thanh toan quoc te cua nguoi nhan
            para.Value = MaNGHangNhan;
            reader = comm.ExecuteReader();

            if (!reader.HasRows)
                return -6; // ngan hang nhan k co trong danh sach lien ket
            reader.Read();
            string userMoiGioi_Nhan = reader.GetString(0).Trim(); //login vao ngan hang cua nguoi gui-->thuc hien rut tien
            string passMoiGioi_Nhan = reader.GetString(1).Trim();
            string maTheMoiGioi_Nhan = reader.GetString(2).Trim();
            reader.Close();
            //end lay ma the TTQT nguoi nhan

            ////////////////////////////////////////////////////////////////////////////////////////
            //giao dich chinh voi cac ngan han//////////////////////////
            //tinh phi giao dich
            string loaiGD = "";
            if (MaNGHangGui == MaNGHangNhan)
                loaiGD = "NoiNH";
            else
                loaiGD = "LienNH";
            SqlCommand comphi = new SqlCommand("SELECT PhiGD FROM PhiGiaoDich WHERE LoaiGD=@loaiGD", newConn);
            SqlParameter para1 = new SqlParameter("@loaiGD", loaiGD);

            comphi.Parameters.Add(para1);
            SqlDataReader readerphi = comphi.ExecuteReader();
            readerphi.Read();
            float phi = float.Parse(readerphi.GetString(0).Trim());

            int phiGiaoDich = imount - (int)(imount * phi);
            readerphi.Close();
            // bat dau goi service chuyen tien de rut tien tu tk gui vao tk moi gioi
            switch (MaNGHangGui)
            {
                case "MSB": //maritime bank
                    maritimebank1.ServiceMaritimeBank mar = new maritimebank1.ServiceMaritimeBank();
                    mar.CookieContainer = new CookieContainer();
                    int kqMar = mar.Transfer(mar.Login(userMoiGioi_Gui, passMoiGioi_Gui), maTheKhachHang, maTheMoiGioi_Gui, imount);
                    if (kqMar <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do Tk tai khoan gui co van de
                    break;

                case "NAB": //Nam A bank
                    namabank1.Service1 nama = new namabank1.Service1();
                    nama.CookieContainer = new CookieContainer();

                    int kqNam = nama.Tranfer(nama.Login(userMoiGioi_Gui, passMoiGioi_Gui), maTheKhachHang, maTheMoiGioi_Gui, imount);
                    if (kqNam <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do k tai khoan gui co van de
                    break;
                case "HDB": //HDbank
                    hdbank.HDBankWS hd = new hdbank.HDBankWS();
                    hd.CookieContainer = new CookieContainer();

                    int kqHD = hd.Tranfer(hd.Login(userMoiGioi_Gui, passMoiGioi_Gui), maTheKhachHang, maTheMoiGioi_Gui, imount);
                    if (kqHD <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do k tai khoan gui co van de
                    break;
            }

            // ket thuc rut tien 

            //bat dau chuyen tien cho tk nhan
            switch (MaNGHangNhan)
            {

                case "NAB"://NamABank
                    namabank1.Service1 nama = new namabank1.Service1();
                    nama.CookieContainer = new CookieContainer();
                    string sidNamA = nama.Login(userMoiGioi_Nhan, passMoiGioi_Nhan);
                    if (sidNamA == "0")
                        return -7; // khong login dc webservice cua ngan hang
                    int kqNam = nama.Tranfer(sidNamA, maTheMoiGioi_Nhan, maTheKinhdoanh, phiGiaoDich);
                    if (kqNam <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "MSB"://maritime bank
                    maritimebank1.ServiceMaritimeBank mar = new maritimebank1.ServiceMaritimeBank();
                    mar.CookieContainer = new CookieContainer();
                    string sidMar = mar.Login(userMoiGioi_Nhan, passMoiGioi_Nhan);
                    if (sidMar == "0")
                        return -7; // khong login dc webservice cua ngan hang
                    int kqMar = mar.Transfer(sidMar, maTheMoiGioi_Nhan, maTheKinhdoanh, phiGiaoDich);
                    if (kqMar <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "HDB"://HD bank
                    hdbank.HDBankWS hd = new hdbank.HDBankWS();
                    hd.CookieContainer = new CookieContainer();

                    string sidHD = hd.Login(userMoiGioi_Nhan, passMoiGioi_Nhan);
                    if (sidHD == "0")
                        return -7; // khong login dc webservice cua ngan hang
                    int kqHD = hd.Tranfer(sidHD, maTheMoiGioi_Nhan, maTheKinhdoanh, phiGiaoDich);
                    if (kqHD <= 0)
                        return -7; // chuyen tien that bai
                    break;
            }
            //ket thuc chuyen tien
            //ket thuc giao dich voi cac ngan hang/////////////////////////////////////////////////
            /////////////////////////////////////////////////////////////////////////////////////////
            //ghi vao lich su thanh toan
            string createquery = "INSERT INTO ThanhToan VALUES (N'" + CustomerCreditCard.Trim()
                                                        + "', N'" + maTheKinhdoanh
                                                        + "', N'" + phiGiaoDich.ToString()
                                                        + "', N'" + DateTime.Now
                                                        + "', N'" + username.Trim()
                                                        + "')";
            SqlCommand createcom = new SqlCommand(createquery, newConn);
            createcom.ExecuteNonQuery();
            newConn.Close();
            return phiGiaoDich; //so phi giao dich

        }

        [WebMethod(EnableSession = true, Description = "chuyen tien cho NXB")]
        public int TranferMoneyForProvider(string sid, string ProviderCreditCard, int imount)
        {
            if (Session[sid.Trim()] == null)
                return -1; //chua dang nhap hoac qua thoi gian dang nhap
            else
                if (Session["LoaiTK"].ToString() != "2" && Session["LoaiTK"].ToString() != "4")// neu la tk ca nhan
                    return -2; // khong co quyen goi service nay
            //end kiem tra login
            // add webservice cua thay

            string username = Session["username"].ToString();
            cardsystem.EMVServices card = new cardsystem.EMVServices();
            card.CookieContainer = new CookieContainer();

            //kiem tra ma the gui thuoc ngan hang nao
            string maTheKinhdoanh = Session[sid].ToString().Trim();
            string MaNGHangGui = card.GetBankCommercialNameOfCreditCard(maTheKinhdoanh);
            if (MaNGHangGui == "CardNotExist")
                return -3; //ma the gui ko hop le
            //end kt ma the gui

            //kiem tra ma the nhan thuoc ngan hang nao

            string MaNGHangNhan = card.GetBankCommercialNameOfCreditCard(ProviderCreditCard.Trim());// thay bang the cua kinh doanh
            if (MaNGHangNhan == "CardNotExist")
                return -4; //ma the nhan ko hop le
            //end kiem tra ma the nhan

            //lay ma the thanh toan quoc te cua ngan hang cua nguoi gui            

            newConn.Open();
            SqlCommand comm = new SqlCommand("SELECT TenDangNhap,MatKhau,MaTheThanhToan FROM TaiKhoan WHERE MaNganHang=@manganhang", newConn);

            SqlParameter para = new SqlParameter("@manganhang", MaNGHangGui);

            comm.Parameters.Add(para);
            SqlDataReader reader = comm.ExecuteReader(); // lay thong tin tai khoan theo Ma ngan hang gui

            if (!reader.HasRows) // neu k co ngan hang
                return -5; // ngan hang gui k co trong danh sach lien ket

            reader.Read();
            string userMoiGioi_Gui = reader.GetString(0).Trim(); //login vao ngan hang cua nguoi gui-->thuc hien rut tien
            string passMoiGioi_Gui = reader.GetString(1).Trim();
            string maTheMoiGioi_Gui = reader.GetString(2).Trim();
            reader.Close();
            // end lay ma the TTQT gui

            // lay ma the thanh toan quoc te cua nguoi nhan
            para.Value = MaNGHangNhan;
            reader = comm.ExecuteReader();

            if (!reader.HasRows)
                return -6; // ngan hang nhan k co trong danh sach lien ket
            reader.Read();
            string userMoiGioi_Nhan = reader.GetString(0).Trim(); //login vao ngan hang cua nguoi gui-->thuc hien rut tien
            string passMoiGioi_Nhan = reader.GetString(1).Trim();
            string maTheMoiGioi_Nhan = reader.GetString(2).Trim();
            reader.Close();
            //end lay ma the TTQT nguoi nhan

            ////////////////////////////////////////////////////////////////////////////////////////
            //giao dich chinh voi cac ngan han//////////////////////////
            //tinh phi giao dich
            string loaiGD = "";
            if (MaNGHangGui == MaNGHangNhan)
                loaiGD = "NoiNH";
            else
                loaiGD = "LienNH";
            SqlCommand comphi = new SqlCommand("SELECT PhiGD FROM PhiGiaoDich WHERE LoaiGD=@loaiGD", newConn);
            SqlParameter para1 = new SqlParameter("@loaiGD", loaiGD);

            comphi.Parameters.Add(para1);
            SqlDataReader readerphi = comphi.ExecuteReader();
            readerphi.Read();
            float phi = float.Parse(readerphi.GetString(0));

            int phiGiaoDich = imount + (int)(imount * phi);
            readerphi.Close();
            // bat dau goi service chuyen tien de rut tien tu tk gui vao tk moi gioi
            switch (MaNGHangGui)
            {
                case "MSB": //maritime bank
                    maritimebank1.ServiceMaritimeBank mar = new maritimebank1.ServiceMaritimeBank();
                    mar.CookieContainer = new CookieContainer();
                    string sidMar = mar.Login(userMoiGioi_Gui, passMoiGioi_Gui);
                    if (sidMar == "0")
                        return -7; //login that bai

                    int kqMar = mar.Transfer(sidMar, maTheKinhdoanh, maTheMoiGioi_Gui, phiGiaoDich);
                    if (kqMar <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do Tk tai khoan gui co van de
                    break;

                case "NAB": //Nam A bank
                    namabank1.Service1 nama = new namabank1.Service1();
                    nama.CookieContainer = new CookieContainer();
                    string sidNamA = nama.Login(userMoiGioi_Gui, passMoiGioi_Gui);
                    if (sidNamA == "0")
                        return -7; //login that bai
                    int kqNam = nama.Tranfer(sidNamA, maTheKinhdoanh, maTheMoiGioi_Gui, phiGiaoDich);
                    if (kqNam <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do k tai khoan gui co van de
                    break;
                case "HDB": //HDbank
                    hdbank.HDBankWS hd = new hdbank.HDBankWS();
                    hd.CookieContainer = new CookieContainer();

                    string sidHD = hd.Login(userMoiGioi_Gui, passMoiGioi_Gui);
                    if (sidHD == "0")
                        return -7; //login that bai
                    int kqHD = hd.Tranfer(sidHD, maTheKinhdoanh, maTheMoiGioi_Gui, phiGiaoDich);
                    if (kqHD <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do k tai khoan gui co van de
                    break;
            }

            // ket thuc rut tien 

            //bat dau chuyen tien cho tk nhan
            switch (MaNGHangNhan)
            {

                case "NAB"://NamABank
                    namabank1.Service1 nama = new namabank1.Service1();
                    nama.CookieContainer = new CookieContainer();
                    string sidNamA = nama.Login(userMoiGioi_Nhan, passMoiGioi_Nhan);
                    if (sidNamA == "0")
                        return -7; // khong login dc webservice cua ngan hang
                    int kqNam = nama.Tranfer(sidNamA, maTheMoiGioi_Nhan, ProviderCreditCard.Trim(), imount);
                    if (kqNam <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "MSB"://maritime bank
                    maritimebank1.ServiceMaritimeBank mar = new maritimebank1.ServiceMaritimeBank();
                    mar.CookieContainer = new CookieContainer();
                    string sidMar = mar.Login(userMoiGioi_Nhan, passMoiGioi_Nhan);
                    if (sidMar == "0")
                        return -7; // khong login dc webservice cua ngan hang
                    int kqMar = mar.Transfer(sidMar, maTheMoiGioi_Nhan, ProviderCreditCard.Trim(), imount);
                    if (kqMar <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "HDB"://HD bank
                    hdbank.HDBankWS hd = new hdbank.HDBankWS();
                    hd.CookieContainer = new CookieContainer();

                    string sidHD = hd.Login(userMoiGioi_Nhan, passMoiGioi_Nhan);
                    if (sidHD == "0")
                        return -7; // khong login dc webservice cua ngan hang
                    int kqHD = hd.Tranfer(sidHD, maTheMoiGioi_Nhan, ProviderCreditCard.Trim(), imount);
                    if (kqHD <= 0)
                        return -7; // chuyen tien that bai
                    break;
            }
            //ket thuc chuyen tien
            //ket thuc giao dich voi cac ngan hang/////////////////////////////////////////////////
            /////////////////////////////////////////////////////////////////////////////////////////
            //ghi vao lich su thanh toan
            string createquery = "INSERT INTO ThanhToan VALUES (N'" + maTheKinhdoanh
                                                        + "', N'" + ProviderCreditCard.Trim()
                                                        + "', N'" + phiGiaoDich.ToString()
                                                        + "', N'" + DateTime.Now
                                                        + "', N'" + username.Trim()
                                                        + "')";
            SqlCommand createcom = new SqlCommand(createquery, newConn);
            createcom.ExecuteNonQuery();
            newConn.Close();
            return phiGiaoDich; //so phi giao dich
        }

        // webmethod thanh toan...
        [WebMethod(EnableSession = true,Description="Payment for Group 5 Bank")]
        public int Payment(string sID, string strCardIDSend, string strCardIDRecieve, int imount)
        {
            //kiem tra login

            if (Session[sID] == null || sID == "0")
                return -1; //chua dang nhap hoac qua thoi gian dang nhap
            string temp = Session["LoaiTK"].ToString();
            if (temp != "3" && temp!="4")//tai khoan cua nhung ngan hang k lien ket se k duoc su dung
                    return -2; // khong co quyen goi service nay
            //end kiem tra login
            // add webservice cua thay
            string username = Session["username"].ToString();
            cardsystem.EMVServices card = new cardsystem.EMVServices();
            card.CookieContainer = new CookieContainer();

            //kiem tra ma the gui thuoc ngan hang nao
            string MaNGHangGui = card.GetBankCommercialNameOfCreditCard(strCardIDSend);
            if (MaNGHangGui == "CardNotExist")
                return -3; //ma the gui ko hop le
            //end kt ma the gui

            //kiem tra ma the nhan thuoc ngan hang nao
            string MaNGHangNhan = card.GetBankCommercialNameOfCreditCard(strCardIDRecieve);
            if (MaNGHangNhan == "CardNotExist")
                return -4; //ma the nhan ko hop le
            //end kiem tra ma the nhan

            //lay ma the thanh toan quoc te cua ngan hang cua nguoi gui
            newConn.Open();
            SqlCommand comm = new SqlCommand("SELECT TenDangNhap,MatKhau,MaTheThanhToan FROM TaiKhoan WHERE MaNganHang=@manganhang", newConn);

            SqlParameter para = new SqlParameter("@manganhang", MaNGHangGui);

            comm.Parameters.Add(para);
            SqlDataReader reader = comm.ExecuteReader(); // lay thong tin tai khoan theo Ma ngan hang gui

            if (!reader.HasRows) // neu k co ngan hang
                return -5; // ngan hang gui k co trong danh sach lien ket

            reader.Read();
            string userMoiGioi_Gui = reader.GetString(0).Trim(); //login vao ngan hang cua nguoi gui-->thuc hien rut tien
            string passMoiGioi_Gui = reader.GetString(1).Trim();
            string maTheMoiGioi_Gui = reader.GetString(2).Trim();
            reader.Close();
            // end lay ma the TTQT gui

            // lay ma the thanh toan quoc te cua nguoi nhan
            para.Value = MaNGHangNhan;
            reader = comm.ExecuteReader();

            if (!reader.HasRows)
                return -6; // ngan hang nhan k co trong danh sach lien ket
            reader.Read();
            string userMoiGioi_Nhan = reader.GetString(0).Trim(); //login vao ngan hang cua nguoi gui-->thuc hien rut tien
            string passMoiGioi_Nhan = reader.GetString(1).Trim();
            string maTheMoiGioi_Nhan = reader.GetString(2).Trim();
            reader.Close();
            //end lay ma the TTQT nguoi nhan

            ////////////////////////////////////////////////////////////////////////////////////////
            //giao dich chinh voi cac ngan han//////////////////////////
            //tinh phi giao dich
            string loaiGD = "";
            if (MaNGHangGui == MaNGHangNhan)
                loaiGD = "NoiNH";
            else
                loaiGD = "LienNH";
            SqlCommand comphi = new SqlCommand("SELECT PhiGD FROM PhiGiaoDich WHERE LoaiGD=@loaiGD", newConn);
            SqlParameter para1 = new SqlParameter("@loaiGD", loaiGD);

            comphi.Parameters.Add(para1);
            SqlDataReader readerphi = comphi.ExecuteReader();
            if (!readerphi.HasRows)
                return -6;
            readerphi.Read();
            float phi = float.Parse(readerphi.GetString(0));

            int phiGiaoDich = imount - (int)(imount * phi);
            readerphi.Close();
            // bat dau goi service chuyen tien de rut tien tu tk gui vao tk moi gioi
            switch (MaNGHangGui)
            {
                case "GPB": //GPbank
                    gpbank.GPBankService gpb = new gpbank.GPBankService();
                    gpb.CookieContainer = new CookieContainer();
                    
                    int kqGpb = gpb.Tranfer(gpb.Login(userMoiGioi_Gui,passMoiGioi_Gui), strCardIDSend, maTheMoiGioi_Gui, imount);
                    if (kqGpb <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do Tk tai khoan gui co van de
                    break;
                case "NAB": //NamABank
                    namabank1.Service1 nab = new namabank1.Service1();
                    nab.CookieContainer = new CookieContainer();
                    int kqNa = nab.Tranfer(nab.Login(userMoiGioi_Gui, passMoiGioi_Gui), strCardIDSend, maTheMoiGioi_Gui, imount);
                    if (kqNa <= 0)
                        return -7;
                    break;
                case "VPB": //VPBank
                    vpbank.Service1 vpb = new vpbank.Service1();
                    vpb.CookieContainer = new CookieContainer();
                    int kqVPB = vpb.Tranfer(vpb.Login(userMoiGioi_Gui, passMoiGioi_Gui), strCardIDSend, maTheMoiGioi_Gui, imount);
                    if (kqVPB <= 0)
                        return -7;
                    break;
                case "CTB":
                    citibank.WebService_NH10 citi = new citibank.WebService_NH10();
                    citi.CookieContainer = new CookieContainer();

                    int kqCiti = citi.Tranfer(citi.Login(userMoiGioi_Gui, passMoiGioi_Gui), strCardIDSend, maTheMoiGioi_Gui, imount);
                    if (kqCiti <= 0)
                        return -7;
                    break;
                case "SAB":
                    seabank.Service1 sea = new seabank.Service1();
                    sea.CookieContainer = new CookieContainer();

                    int kqSea = sea.Tranfer(sea.Login(userMoiGioi_Gui, passMoiGioi_Gui), strCardIDSend, maTheMoiGioi_Gui, imount);
                    if (kqSea <= 0)
                        return -7;
                    break;


               
            }

            // ket thuc rut tien 

            //bat dau chuyen tien cho tk nhan
            switch (MaNGHangNhan)
            {
                case "GPB": //GPbank
                    gpbank.GPBankService gpb = new gpbank.GPBankService();
                    gpb.CookieContainer = new CookieContainer();

                    int kqGpb = gpb.Tranfer(gpb.Login(userMoiGioi_Nhan, passMoiGioi_Nhan),maTheMoiGioi_Nhan,strCardIDRecieve,phiGiaoDich);
                    if (kqGpb <= 0) // k nhan duoc tien
                        return -7; // thanh toan that bai do Tk tai khoan gui co van de
                    break;

                case "NAB"://NamABank
                    namabank1.Service1 nama = new namabank1.Service1();
                    nama.CookieContainer = new CookieContainer();

                    int kqNam = nama.Tranfer(nama.Login(userMoiGioi_Nhan, passMoiGioi_Nhan), maTheMoiGioi_Nhan, strCardIDRecieve, phiGiaoDich);
                    if (kqNam <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "VPB"://VPBank
                    vpbank.Service1 vpb = new vpbank.Service1();
                    vpb.CookieContainer = new CookieContainer();

                    int kqVpb = vpb.Tranfer(vpb.Login(userMoiGioi_Nhan, passMoiGioi_Nhan), maTheMoiGioi_Nhan, strCardIDRecieve, phiGiaoDich);
                    if (kqVpb <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "CTB":
                    citibank.WebService_NH10 citi = new citibank.WebService_NH10();
                    citi.CookieContainer = new CookieContainer();
                    int kqCiti = citi.Tranfer(citi.Login(userMoiGioi_Nhan, passMoiGioi_Nhan), maTheMoiGioi_Nhan, strCardIDRecieve, phiGiaoDich);
                    if (kqCiti <= 0)
                        return -7; // chuyen tien that bai
                    break;
                case "SAB":
                    seabank.Service1 sea = new seabank.Service1();
                    sea.CookieContainer = new CookieContainer();

                    int kqSea = sea.Tranfer(sea.Login(userMoiGioi_Nhan, passMoiGioi_Nhan), maTheMoiGioi_Nhan, strCardIDRecieve, phiGiaoDich);
                    if (kqSea <= 0)
                        return -7;
                    break;

                                    
            }
            //ket thuc chuyen tien
            //ket thuc giao dich voi cac ngan hang/////////////////////////////////////////////////
            /////////////////////////////////////////////////////////////////////////////////////////
            //ghi vao lich su thanh toan
            string createquery = "INSERT INTO ThanhToan VALUES (N'" + strCardIDSend.Trim()
                                                        + "', N'" + strCardIDRecieve.Trim()
                                                        + "', N'" + phiGiaoDich.ToString()
                                                        + "', N'" + DateTime.Now
                                                        + "', N'" + username.Trim()
                                                        + "')";
            SqlCommand createcom = new SqlCommand(createquery, newConn);
            createcom.ExecuteNonQuery();
            newConn.Close();
            return phiGiaoDich; //so phi giao dich
        }
        // random sid 
        public string generateRandomString(int length)
        {
            string randomString = "";
            int randNumber;
            Random rnd = new Random();

            for (int i = 0; i < length; i++)
            {
                if (rnd.Next(1, 3) == 1)
                    randNumber = rnd.Next(97, 103); //char {a-z}
                else
                    randNumber = rnd.Next(48, 58); //int {0-9}

                randomString = randomString + (char)randNumber;
            }
            return randomString;
        }
    }
}